//
//  HWVIPPrivilegeCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/8/29.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWVIPPrivilegeCell: UITableViewCell {
    var bgView:UIView!
    var model:HWNewVIPModel?{
        didSet {
            var tempView:HWVIPPrivilegeView!
            for (idx,mod) in (model?.shuoming ?? []).enumerated() {
                let privilegeView = HWVIPPrivilegeView()
                privilegeView.logoImageView.kf.setImage(with: URL(string: mod.icon))
                privilegeView.titleLabel.text = mod.title
                privilegeView.contentLabel.text = mod.content
                bgView.addSubview(privilegeView)
                if tempView == nil {
                    privilegeView.snp.makeConstraints { make in
                        make.left.top.equalTo(18*widthScale)
                        make.right.equalTo(-18*widthScale)
                    }
                } else {
                    privilegeView.snp.makeConstraints { make in
                        make.top.equalTo(tempView.snp.bottom).offset(9*widthScale)
                        make.left.equalTo(18*widthScale)
                        make.right.equalTo(-18*widthScale)
                        if idx == (model?.shuoming.count ?? 0) - 1 {
                            make.bottom.equalToSuperview()
                        }
                    }
                }
                
                tempView = privilegeView
                
            }
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWVIPPrivilegeCell) {
        let reUse:String = "HWVIPPrivilegeCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWVIPPrivilegeCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWVIPPrivilegeCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }

    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .colorWithHexString(hexString: "#2E2E2E")
        bgView.layer.cornerRadius = 14*widthScale
        bgView.layer.masksToBounds = true
        contentView.addSubview(bgView)
        
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(12*widthScale).priority(.high)
            make.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
